package cn.edu.scujj.view;
import cn.edu.scujj.bean.Breakage;
import cn.edu.scujj.dao.BreakageDao;

import javax.swing.*;
import javax.swing.table.AbstractTableModel;
import java.awt.*;
import java.util.Vector;
import java.util.List;
public class BreakageUI extends JFrame {
    JScrollPane jScrollPane = new JScrollPane();
    AbstractTableModel tableModel ;
    Vector vector = new Vector();

    JTextField textField1 = new JTextField(4);
    JTextField textField2 = new JTextField(4);
    JTextField textField3 = new JTextField(4);
    JTextField textField4 = new JTextField(4);
    BreakageDao dao = new BreakageDao();
    public BreakageUI () {
        setTitle("图书损坏管理");
        setBounds(100,100,800,600);
        setLayout(new BorderLayout());

        JPanel panel = new JPanel();
        JPanel panelUP= new JPanel();
        JPanel panelDown = new JPanel();
        JPanel panelCenter = new JPanel();

        panelUP.add(jScrollPane);
        createTable();

        add(panelUP,BorderLayout.NORTH);
        add(panelCenter,BorderLayout.CENTER);
        add(panelDown,BorderLayout.SOUTH);


        panelCenter.add(new JLabel("损坏id"));
        panelCenter.add(textField1);
        panelCenter.add(new JLabel("图书id"));
        panelCenter.add(textField2);
        panelCenter.add(new JLabel("损坏时间"));
        panelCenter.add(textField3);
        panelCenter.add(new JLabel("损坏数量"));
        panelCenter.add(textField4);
        panelCenter.add(Box.createVerticalStrut(100)); // 添加换行
        JButton btnAdd = new JButton("添加");
        btnAdd.addActionListener(e -> addDate());
        JButton btnchax = new JButton("查询");
        btnchax.addActionListener(e -> query());
        JButton btnXiugai = new JButton("修改");
        btnXiugai.addActionListener(e -> update());
        JButton btnDelete = new JButton("删除");
        btnDelete.addActionListener(e -> delete());

        panelDown.add(btnAdd);
        panelDown.add(btnchax);
        panelDown.add(btnXiugai);
        panelDown.add(btnDelete);


        select();
    }



    public void addDate() {
        BreakageDao dao = new BreakageDao();
        Breakage breakage = new Breakage();
        breakage.setBreakageId(Integer.parseInt(textField1.getText()));
        breakage.setBookId(Integer.parseInt(textField2.getText()));
        breakage.setBreakageTime(textField3.getText());
        breakage.setBreakageSum(Integer.parseInt(textField4.getText()));
        boolean result;
        if (dao.addBreakage(breakage)) result = true;
        else result = false;
        if (!result) {
            JOptionPane.showMessageDialog(this, "添加失败，id不能重复");
        } else {
            JOptionPane.showMessageDialog(this, "添加成功");
            select();
        }
        textField1.setText("");
        textField2.setText("");
        textField3.setText("");
        textField4.setText("");

    }

    public void query() {
        String book_id = textField2.getText().trim(); // book_id 输入框
        String breakage_time = textField3.getText().trim(); // 损坏时间输入框

        if (book_id.isEmpty() && breakage_time.isEmpty()) {
            JOptionPane.showMessageDialog(this, "请输入 图书id 或 损坏时间");
            return;
        }

        List<Breakage> breakages;
        if (!book_id.isEmpty()) {
            try {
                int id = Integer.parseInt(book_id);
                breakages = dao.queryBreakages(id, true); // 按 book_id 查询
            } catch (NumberFormatException e) {
                JOptionPane.showMessageDialog(this, "book_id 必须为数字");
                return;
            }
        } else {
            breakages = dao.queryBreakages(breakage_time, false); // 按时间模糊查询
        }

        vector.clear();
        for (Breakage breakage : breakages) {
            Vector<Object> v = new Vector<>();
            v.add(breakage.getBreakageId());
            v.add(breakage.getBookId());
            v.add(breakage.getBreakageTime());
            v.add(breakage.getBreakageSum());
            vector.add(v);
        }

        tableModel.fireTableDataChanged();
        JOptionPane.showMessageDialog(this, "查询成功，共 " + breakages.size() + " 条记录");
        textField1.setText("");
        textField2.setText("");
        textField3.setText("");
        textField4.setText("");
    }


    public void update() {
        BreakageDao dao = new BreakageDao();

        int breakageId;
        int breakageSum;

        try {
            breakageId = Integer.parseInt(textField1.getText());     // 获取 breakage_id
            breakageSum = Integer.parseInt(textField4.getText());    // 获取新的 breakage_sum
        } catch (NumberFormatException e) {
            JOptionPane.showMessageDialog(this, "请输入有效的数字");
            return;
        }

        Breakage breakage = new Breakage();
        breakage.setBreakageId(breakageId);
        breakage.setBreakageSum(breakageSum);
        if (dao.updateBreakage(breakage)) {
            JOptionPane.showMessageDialog(this, "损坏数量修改成功");
            select(); // 刷新表格
        } else {
            JOptionPane.showMessageDialog(this, "损坏数量修改失败");
        }
    }

    public void delete() {
        BreakageDao dao = new BreakageDao();
        int breakageId;

        try {
            breakageId = Integer.parseInt(textField1.getText()); // 获取 breakage_id
        } catch (NumberFormatException e) {
            JOptionPane.showMessageDialog(this, "请输入有效的 图书损坏id");
            return;
        }

        if (dao.deleteBreakage(breakageId)) {
            JOptionPane.showMessageDialog(this, "删除成功");
            select(); // 刷新表格
        } else {
            JOptionPane.showMessageDialog(this, "删除失败");
        }
    }

    private void createTable() {
        String[] columnNames = {"损坏ID","图书ID","损坏时间","损坏数量"};
        JTable table = new JTable();
        jScrollPane.setViewportView(table);
        tableModel = new AbstractTableModel() {
            @Override
            public int getRowCount() {
                return vector.size();
            }

            @Override
            public int getColumnCount() {
                return columnNames.length;
            }

            @Override
            public Object getValueAt(int rowIndex, int columnIndex) {
                return ((Vector)vector.get(rowIndex)).get(columnIndex);
            }

            @Override
            public String getColumnName(int column) {
                return columnNames[column];
            }
        };
        table.setModel(tableModel);
    }

    void select() {
        vector.clear();
        BreakageDao dao = new BreakageDao();
        List<Breakage> list = dao.findAll();
        for (Breakage breakage : list) {
            Vector v = new Vector();
            v.add(breakage.getBreakageId());
            v.add(breakage.getBookId());
            v.add(breakage.getBreakageTime());
            v.add(breakage.getBreakageSum());
            vector.add(v);
        }
        tableModel.fireTableDataChanged();
    }

    public static void main(String[] args) {
        new BreakageUI().setVisible(true);
    }

}

